/**
 * 
 */
package DBHandler;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;

/**
 * Get record in PMC full-text XML format with an identifier from Database.
 *
 */
public class PostgreSQLJDBC {
	public static boolean saveXMLtoDB(String pmcid,String xmlcontent)
	{
		Connection c = null;
		try {
	     Class.forName("org.postgresql.Driver");
		 c = DriverManager
		    .getConnection("jdbc:postgresql://localhost:5432/IDP",
		"username", "password");
		 c.setAutoCommit(false);
		 System.out.println("Opened database successfully");
		 
		 CallableStatement stmt = c.prepareCall("{call savefulltextxmls(?,?,?)}");
		 stmt.registerOutParameter(3, Types.VARCHAR);
		 stmt.setObject(1, pmcid );
		 stmt.setObject(2,xmlcontent);
		 stmt.execute();
		 String results = (String) stmt.getObject(3);
		 
		 System.out.println("\nThe result is " + results);
		 System.out.println("Records created successfully");
	     c.commit();
	     c.close();
	     return true;
	    }
	    catch (SQLException se) {
	      System.out.println("\nA SQL exception occurred.");
	      System.out.println("\nError code: " + se.getErrorCode());
	      System.out.println("\nSQL state: " + se.getSQLState());
	      se.printStackTrace();
	      return false;
	    }
	    catch (Exception e) {
	    	System.err.println( e.getClass().getName()+": "+ e.getMessage() );
	    	e.printStackTrace();
	    	return false;
	    }
	}
	public static boolean saveProtMutPMCIDtoDB(String organism,String protein,String pubmedID,String pmcID)
	{
		Connection c = null;
		try {
	     Class.forName("org.postgresql.Driver");
		 c = DriverManager
		    .getConnection("jdbc:postgresql://localhost:5432/IDP",
		"postgres", "skarn");
		 c.setAutoCommit(false);
		 System.out.println("Opened database successfully");
		 
		 CallableStatement stmt = c.prepareCall("{call saveprotmutatpmcid(?,?,?,?,?)}");
		 stmt.registerOutParameter(5, Types.VARCHAR);
		 stmt.setObject(1, organism);
		 stmt.setObject(2, protein );
		 stmt.setObject(3,pubmedID);
		 stmt.setObject(4,pmcID);
		 stmt.execute();
		 String results = (String) stmt.getObject(5);
		 
		 System.out.println("\nThe result is " + results);
		 System.out.println("Records created successfully");
	     c.commit();
	     c.close();
	     return true;
	    }
	    catch (SQLException se) {
	      System.out.println("\nA SQL exception occurred.");
	      System.out.println("\nError code: " + se.getErrorCode());
	      System.out.println("\nSQL state: " + se.getSQLState());
	      se.printStackTrace();
	      return false;
	    }
	    catch (Exception e) {
	    	System.err.println( e.getClass().getName()+": "+ e.getMessage() );
	    	e.printStackTrace();
	    	return false;
	    }
	}

}
